const log4js = require('log4js')

const defaultConfig = {
	appenders: {
		out: {
			type: 'stdout',
			layout: {
				type: 'pattern',
				pattern: '%d{[yyyy-MM-dd hh:mm:ss]} %c %p %f{1}:%l %m%n'
			}
		},
		multi: {
			type: 'multiFile',
			layout: {
				type: 'pattern',
				pattern: '%d{[yyyy-MM-dd hh:mm:ss]} %c %p %f{1}:%l %m%n'
			},
			base: 'logs/',
			property: 'categoryName',
			extension: '.log'
		}
	},
	categories: {
		default: { appenders: ['out', 'multi'], level: 'info', enableCallStack: true }
	}
}

module.exports = (category, config = {}) => {
	if (Object.prototype.toString.call(config) !== '[object Object]') throw new Error('logger config should be object!')

	log4js.configure(Object.assign({}, defaultConfig, config))
	return log4js.getLogger(category)
}
